package cn.yunyichina.openapi.web.controller;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.yunyichina.provider.framework.dubbo.service.BaseService;
import cn.yunyichina.provider.iface.entity.common.Response;
import cn.yunyichina.utils.convert.JsonUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.yunyichina.openapi.web.util.StringHelper;

import com.google.gson.Gson;

/**
 * 
 * @author zhangkewen
 * 
 */
@Controller
public class ApiController extends BaseController {

	private static final String BASE_API_LIST = "yyt.base.api.list";
	private static final String BASE_APICLASS_LIST = "yyt.base.apiclass.list";
	private static final String BASE_API_PARAMSTRUCT_LIST = "yyt.base.apiParamstruct.list";
	private static final String BASE_PARAMSTRUCT_LIST = "yyt.base.paramstruct.list";
	private static final String BASE_PARAMSTRUCT_WITH_DETAIL = "yyt.base.paramstruct.getByStruct";
	private static final String BASE_ERRORCODE_LIST = "yyt.base.errorcode.list";
//	@Autowired
//	private ApiServices apiServices;

	/**
	 * 获取所有接口的分类
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping("getApiClass.do")
	public void getApiClass(HttpServletRequest request,
			HttpServletResponse response) {
		String apiclassid = request.getParameter("apiclassid");

		Response serviceResponse = BaseService.callHumpFromInternal(
				BASE_APICLASS_LIST, null
		);
		String result = serviceResponse.getResult();
//		List<Apiclass> apisses = JsonUtils.parseObject(result, List.class);
//		Gson gson = new Gson();
		writetoPage(response, result);
	}

	/**
	 * 获取接口分类下面的接口明细
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping("getApi.do")
	public void getApi(HttpServletRequest request, HttpServletResponse response) {

		String apiClassId = request.getParameter("apiClassId");
		String id = request.getParameter("apiId");
		String apiname = request.getParameter("apiname");

		Map<String, Object> params = new HashMap<String, Object>();
		if (apiClassId != null && !apiClassId.isEmpty()) {
			params.put("api_class_id", apiClassId);
		} else if (id != null && !id.isEmpty()) {
			params.put("id", id);
		} else if (apiname != null && !apiname.isEmpty()) {
			try {
				apiname = java.net.URLDecoder.decode(apiname, "UTF-8");
			} catch (UnsupportedEncodingException e) {
				System.out.println(e);
			}
			params.put("apiname", apiname);
		}

		Response serviceResponse = BaseService.callHumpFromInternal(
				BASE_API_LIST, JsonUtils.toJsonString(params)
		);
		writetoPage(response,  serviceResponse.getResult());
	}

	/**
	 * 获取接口下面的结构
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping("getApistruct.do")
	public void getApistruct(HttpServletRequest request,
			HttpServletResponse response) {

		String apiId = request.getParameter("apiId");
		String apiStructType = request.getParameter("apiStructType");

		if (StringUtils.isNotBlank(apiId) && StringHelper.isNumeric(apiStructType)) {

			Map<String, String> params = new HashMap<String, String>();
			params.put("sys_api_id", apiId);
			if (Integer.parseInt(apiStructType) > 0)
				params.put("api_struct_type", apiStructType);

			Response serviceResponse = BaseService.callHumpFromInternal(
					BASE_API_PARAMSTRUCT_LIST, JsonUtils.toJsonString(params)
			);
			String result = serviceResponse.getResult();
			writetoPage(response,result);
		} else {
			writetoPage(response, "{\"ret\":1,\"message\":\"参数错误\"}");
		}
	}

	/**
	 * 获取结构
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping("getParamstruct.do")
	public void getParamstruct(HttpServletRequest request,
			HttpServletResponse response) {

		String structId = request.getParameter("structId");
		if (StringUtils.isNotBlank(structId)) {
			String params = "{\"struct_id\":\"" + structId + "\"}" ;
			Response serviceResponse = BaseService.callHumpFromInternal(
					BASE_PARAMSTRUCT_LIST, params);
			String result = serviceResponse.getResult();
			writetoPage(response,result);
		} else {
			writetoPage(response, "{\"ret\":1,\"message\":\"参数错误\"}");
		}
	}

	/**
	 * 获取该结构下的所有参数
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping("getParametersByStruct.do")
	public void getParametersByStruct(HttpServletRequest request,
			HttpServletResponse response) {

		String structId = request.getParameter("structId");
		if (StringUtils.isNotBlank(structId)) {
			String params = "{\"id\":\"" + structId + "\"}" ;
			Response serviceResponse = BaseService.callHumpFromInternal(
					BASE_PARAMSTRUCT_WITH_DETAIL, params);
			String result = serviceResponse.getResult();
			writetoPage(response,result);
		} else {
			writetoPage(response, "{\"ret\":1,\"message\":\"参数错误\"}");
		}
	}

	/**
	 * 获取所有异常编码
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping("getErrorcode.do")
	public void getErrorcode(HttpServletRequest request,
			HttpServletResponse response) {
		Response serviceResponse = BaseService.callHumpFromInternal(
				BASE_ERRORCODE_LIST, null);
		String result = serviceResponse.getResult();
		writetoPage(response,result);
	}

	@RequestMapping("getToken.do")
	public void getToken(HttpServletRequest request,
			HttpServletResponse response) {

		String url = request.getParameter("url");
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		//String result = ApiUtil.login(url, username, password);
		//writetoPage(response, result);
	}

	@RequestMapping("methodInvoke.do")
	public void methodInvoke(HttpServletRequest request,
			HttpServletResponse response) {

		String url = request.getParameter("url");
		String username = request.getParameter("username");
		String session = request.getParameter("session");
		String appkey = request.getParameter("appkey");
		String method = request.getParameter("method");
		String data = request.getParameter("data");

//		String result = ApiUtil.MethodInvoke(url, username, session, appkey,
//				method, data);
//		writetoPage(response, result);
	}

}
